package com.za.apm.cat.config;

import javax.servlet.ServletRequestListener;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 类WebConfig.java的实现描述：
 * 
 * @author: tongyufu 2017-11-22 10:50:01
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new CatInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}

	/**
	 * Cat Filter
	 */
	// @Bean
	public FilterRegistrationBean catFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		CatFilter filter = new CatFilter();
		registration.setFilter(filter);
		registration.addUrlPatterns("/*");
		registration.setName("cat-filter");
		registration.setOrder(1);
		return registration;
	}

	@Bean
	public ServletListenerRegistrationBean<ServletRequestListener> servletListenerRegistrationBean() {
		ServletListenerRegistrationBean<ServletRequestListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<ServletRequestListener>();
		servletListenerRegistrationBean.setListener(new RequestContextListener());
		return servletListenerRegistrationBean;
	}
}
